package com.example.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.demo.entity.Comment;
import com.example.demo.entity.User;
import com.example.demo.exception.ApplicationException;
import com.example.demo.mapper.CommentMapper;
import com.example.demo.mapper.UserMapper;
import com.example.demo.service.CommentService;
import com.example.demo.service.OrderService;
import com.example.demo.vo.Result;
import com.example.demo.vo.ResultCode;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
* @author 15085202591
* @description 针对表【comment】的数据库操作Service实现
* @createDate 2024-06-06 08:54:28
*/
@Slf4j
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService{

    @Resource
    private CommentMapper commentMapper;

    @Resource
    private UserMapper userMapper;

    @Resource
    private OrderService orderService;

    @Override
    public void addComment(Long userId, Long goodsId, String commentText , Long orderId ,Integer plusComment) {
        // 1. 校验参数
        if (userId < 1 || goodsId < 1 || commentText == null) {
            log.warn("参数错误");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }


        // 2. 根据用户id查出用户对象
        User user = userMapper.selectById(userId);
        // 校验用户状态
        if (user == null || user.getIsDeleted() == 1) {
            log.warn("用户不存在!");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0005));
        }
        if (user.getStatus() == 0) {
            log.warn("用户状态异常");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0006));
        }

        // 3. 构造评论对象
        Comment comment = new Comment();
        comment.setUserId(userId);
        comment.setGoodsId(goodsId);
        comment.setCommentText(commentText);
        // 设置用户名
        comment.setUserName(user.getUsername());
        // 设置时间
        LocalDateTime dateTime = LocalDateTime.now();
        comment.setCommentTime(dateTime);
        comment.setCreateTime(dateTime);
        comment.setUpdateTime(dateTime);
        comment.setUserAvatar(user.getAvatarPath());

        // 4. 调用mapper
        int row = commentMapper.insert(comment);
        if (row != 1) {
            log.warn("评价失败");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A9999));
        }
        //5.更新订单表的是否评价字段
        orderService.updateCommit(plusComment,orderId);

        // 6. 评价成功
        log.info("成功评价!");

    }

    @Override
    public List<Comment> getCommonByGoodsId(Long goodsId) {
        // 1. 校验参数
        if (goodsId < 1) {
            log.warn("参数错误");
            throw new ApplicationException(Result.error(ResultCode.CLIENT_A0002));
        }

        // 2. 调用mapper
        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("goods_id", goodsId);
        List<Comment> comment = ( commentMapper.selectList(wrapper));
        log.info("查询成功");
        return comment;
    }
}




